<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Mark extends CI_Model{
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    public function get_idtester()
    {
        $sql="select distinct id_te from qsubmit";
        $query=$this->db->query($sql);
        return $query;
    }
    public function get_submit()
    {
        $sql="select * from qsubmit group by id_te";
        $query=$this->db->query($sql);
        return $query;
    }
    public function get_ans_byid($id_te)
    {
        $this->db->select('*');
        $this->db->from('qsubmit');
        $this->db->order_by('id_question');
        $this->db->where('id_te',$id_te);
        $query=$this->db->get();
        return $query;
    }
    public function insert_to_tester($id_te)
    {
        $data = array(  
            'id_te'=>$id_te              
                    );
        if($this->db->insert('tester',$data))
                return 1;
        else            
                return 0;
    }
    public function get_mark_byidques($id_question)
    {
        $this->db->select('*');
        $this->db->from('qsubmit');
        $this->db->where('id_question',$id_question);
        $query=$this->db->get();
        return $query;
    }
    public function get_qsubmit($id_te,$test_id)
    {
        $this->db->select('*');
        $this->db->from('qsubmit');
        $this->db->where('id_te',$id_te);
        $this->db->where('test_id',$test_id);
        $this->db->order_by('id_question', 'asc'); 
        $query=$this->db->get();
        return $query;
    }
    public function get_answerx1_byid($id_question)
    {
       $this->db->select('*');
       $this->db->from('x1_q');
       $this->db->where('id_question',$id_question);
       $query=$this->db->get();
       return $query; 
    }
    public function get_answerx2_byid($id_question)
    {
       $this->db->select('*');
       $this->db->from('x2_q');
       $this->db->where('id_question',$id_question);
       $query=$this->db->get();
       return $query; 
    }
    public function get_answerx3_byid($id_question)
    {
       $this->db->select('*');
       $this->db->from('x3_q');
       $this->db->where('id_question',$id_question);
       $query=$this->db->get();
       return $query; 
    }
    public function check_mark($id_question,$id_te,$test_id)
    {
        $this->db->select('*');
        $this->db->from('qsubmit');
        $this->db->where('id_question',$id_question);
        $this->db->where('id_te',$id_te);
        $this->db->where('test_id',$test_id);
        $query=$this->db->get();
        return $query;
    }
    public function insert_mark($id_question,$id_te,$test_id,$mark_ques)
    {
        $data = array('id_question'=>$id_question,
                    'id_te'=>$id_te,
                    'test_id'=>$test_id,
                    'mark_ques'=>$mark_ques
                );
	$this->db->insert('qsubmit',$data);
    }
    public function update_mark($id_question,$id_te,$test_id,$mark_ques)
    {
        $data = array(
                    'mark_ques'=>$mark_ques
                );
	$this->db->where('id_question', $id_question);
        $this->db->where('id_te', $id_te);
        $this->db->where('test_id', $test_id);
        $this->db->update('qsubmit', $data);
    }
    public function update_sum_mark($id_te,$testtitle,$sum_mark)
    {
        $data = array(
                    'sum_mark'=>$sum_mark
                );
	$this->db->where('id_te', $id_te);
        $this->db->where('testtitle', $testtitle);
        $this->db->update('tester', $data);
    }
    public function get_tester_bymaker($id_mk)
    {
        $this->db->select('*');
        $this->db->from('tester');
        $this->db->where('id_mk',$id_mk);
        $query = $this->db->get();
        return $query;
    }
    public function used_bymaker($id_mk)
    {
        /*$this->db->select('*');
        $this->db->from('qsubmit');
        $this->db->where('mark_ques','採点者要介');
        $this->db->where('id_te',$id_te);
        $query = $this->db->get();*/
        $sql1="SELECT id_te FROM tester WHERE id_mk='$id_mk'";
        $query1=$this->db->query($sql1);
        $id_te=$query1->result();
        $id=array();
        foreach($id_te as $row)
        {
            array_push($id, $row->id_te);
        }
        //$query=$this->db->query($sql);
        $this->db->select('*');
        $this->db->from('qsubmit');
        $this->db->where('mark_ques','採点者要介');
        $this->db->where_in('id_te',$id);
        $query = $this->db->get();
        return $query;
    }
    public function view_test($id_test,$id_mk)
    {
        $this->db->select('*');
        $this->db->from('tester');
        $this->db->where('testtitle',$id_test);
        $this->db->where('id_mk',$id_mk);
        $query = $this->db->get();
        return $query;
    }
    public function update_mark_bymaker($id_question,$id_te,$test_id,$mark_ques)
    {
        $data = array(
                    'mark_ques'=>$mark_ques
                );
        $this->db->where('id_question', $id_question);
	$this->db->where('id_te', $id_te);
        $this->db->where('test_id', $test_id);
        $this->db->update('qsubmit', $data);
    }
    public function get_sum_mark($id_te,$testtitle)
    {
        $this->db->select('*');
        $this->db->from('tester');
        $this->db->where('id_te',$id_te);
        $this->db->where('testtitle',$testtitle);
        $query=$this->db->get();
        return $query;
    }
}
?>
